/**
 * 
 */
package com.res.simulator.handler;

import com.res.common.handler.CommonHandler;
import com.res.simulator.model.CommandExecutorModel;
import com.res.simulator.plugins.MessageCommunicationClientPlugin;
import com.res.simulator.view.CommandExecutorView;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.utils.CommonUtils;

/**
 * @author Dhanushka Ariyasingha.
 *
 */
public class CommandExecutorHandler implements	CommonHandler<CommandExecutorView, CommandExecutorModel> {

	public CommandExecutorView process(final CommandExecutorModel model)
			throws Exception {

		final CommunicationMessage message = extract(model);
		
		final MessageCommunicationClientPlugin plugin = new MessageCommunicationClientPlugin();
		final CommunicationMessage returnedMessage = plugin.invoke(message);
		
		final CommandExecutorView view =new CommandExecutorView();
		update(view,returnedMessage.getMessageBody());
		
		return view;
	}
	
	public void update(final CommandExecutorView view, final String valueString) throws Exception{
		
		final String command = CommonUtils.find(valueString, "COMMAND");
		final String result = CommonUtils.find(valueString, "RESULT");		
		final String value = CommonUtils.find(valueString, "VALUE");
		
		view.setCommand(command);
		view.setResult(result);
		view.setValue(value);
	}

	/**
	 * Value String -
	 *USER_ID:|PAYMENT_METHOD:VOLBILL|SHIPPING:NULL|HMEC:NO|SERVICE_ACTIVATE:NO|COS:NO|TOS:NO|USER_OFFER_EXTERNAL_ID:
	 * 
	 * @param model
	 * @return
	 * @throws Exception 
	 */
	private CommunicationMessage extract(final CommandExecutorModel model) throws Exception {

		
		final CommunicationMessage message = new CommunicationMessage();
		final String valueString = model.getCommandString();
		
		
		message.setMessageHeader("JOBType:CommandExecution|ID:dev44.aepona.com");
		message.setMessageBody(valueString);
		
		return message;
	}
}
